<?php
namespace controllers;

use components\Base;

class BaseController extends Base
{
	//初始化解析参数
	public function __construct($requestParams = []){
		foreach ($requestParams  as $k => $val ) {
			$this->params[$k] = $val;
		}
	}
	//参数校验
	public function paramCheck($action){
		//paramRule()
		$reflectClass  = new \ReflectionClass($this);
		if ($reflectClass->hasMethod('getParamRules')) {
			$params = $this->getParamRules();
			if ($params && isset($params[$action])) {
				$params = $params[$action];
				foreach ($params as $func => $p) {
					if (!isset($this->params[$p])) {
						throw new \Exception('paramCheck request params not exist:[param]:'. $p);
					}
					if (function_exists($func)) {
						if (!$func($this->params[$p])) {
							throw new \Exception('paramCheck request params not accord rule:[param]:'. $p . '[rule]:'. $func);
						}
					}
				}
			}
		}
	}
}